package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.li.work.mapper.SysUserMapper;
import com.ruoyi.teaching.domain.PlatformFForum;
import com.ruoyi.teaching.domain.PlatformFZone;
import com.ruoyi.teaching.domain.UniversalCreateUpdate;
import com.ruoyi.teaching.mapper.PlatformFForumMapper;
import com.ruoyi.teaching.mapper.PlatformFZoneMapper;
import com.ruoyi.teaching.service.PlatformFZoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.teaching.utils.UniversalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 论坛专区管理 服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2024-10-14
 */
@Service
public class PlatformFZoneServiceImpl extends ServiceImpl<PlatformFZoneMapper, PlatformFZone> implements PlatformFZoneService {

    @Autowired
    private PlatformFZoneMapper platformFZoneMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public AjaxResult zoneList(Integer pageNum, Integer pageSize, String zoneName,Integer sort) {
        Page<PlatformFZone> platformFZonePage = new Page<>(pageNum, pageSize);
        QueryWrapper<PlatformFZone> platformFZoneQueryWrapper = new QueryWrapper<>();
        if(!"".equals(zoneName) && null != zoneName){
            platformFZoneQueryWrapper.like("zone_name", zoneName);
        }
        if(sort != null){
            platformFZoneQueryWrapper.orderByAsc("create_time");
        }else {
            platformFZoneQueryWrapper.orderByDesc("create_time");
        }
        platformFZoneQueryWrapper.eq("del_flag",0);
        Page<PlatformFZone> platformFZonePage1 = platformFZoneMapper.selectPage(platformFZonePage, platformFZoneQueryWrapper);
        //如果专业大类的名称在platform_c_specialty修改，那么在platform_f_zone也要及时修改
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", platformFZonePage1);
    }

    @Override
    public AjaxResult addZone(PlatformFZone platformFZone) {
        UniversalCreateUpdate create = UniversalUtil.getCreateUpdate(sysUserMapper);
        platformFZone.setCreateBy(create.getName());
        platformFZone.setCreateTime(DateUtils.getNowDate());
        platformFZone.setCreateId(create.getId());
        int insert = platformFZoneMapper.insert(platformFZone);
        if (insert > 0) {
            return new AjaxResult(HttpStatus.SUCCESS, "添加成功");
        } else {
            return new AjaxResult(HttpStatus.ERROR, "添加失败");
        }
    }

    @Override
    public AjaxResult updateZone(PlatformFZone platformFZone) {
        // TODO 如果platform_c_specialty修改名称，这里存的specialtyName字符串也要及时修改
        UniversalCreateUpdate update = UniversalUtil.getCreateUpdate(sysUserMapper);
        platformFZone.setUpdateBy(update.getName());
        platformFZone.setUpdateTime(DateUtils.getNowDate());
        platformFZone.setUpdateId(update.getId());
        int i = platformFZoneMapper.updateById(platformFZone);
        if (i > 0) {
            return new AjaxResult(HttpStatus.SUCCESS, "修改成功");
        } else {
            return new AjaxResult(HttpStatus.ERROR, "修改失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public AjaxResult deleteZoneByIds(Long[] ids) {
        int delCount = 0;
        for (Long id : ids) {
            PlatformFZone platformFZone = new PlatformFZone();
            platformFZone.setId(id);
            platformFZone.setDelFlag(1);
            int i = platformFZoneMapper.updateById(platformFZone);
            if(i > 0){
                delCount++;
            }
        }
        if(delCount == ids.length){
            return new AjaxResult(HttpStatus.SUCCESS, "删除成功");
        }
        throw new RuntimeException("删除失败");
    }
}
